package handlers

import (
	"car-test-syss/cache"
	"car-test-syss/models"
	"encoding/json"
	"github.com/gin-gonic/gin"
	"strconv"
	"time"
)

func CarAdd(c *gin.Context) {
	car_code := c.PostForm("car_code")
	car_name := c.PostForm("car_name")
	car_num := c.PostForm("car_num")
	car := models.Car{
		CarCode: car_code,
		CarName: car_name,
		CarNo:   car_num,
	}
	models.DB.Create(&car)
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "添加成功",
	})
	return
}
func OrderAdd(c *gin.Context) {
	order_no := c.PostForm("car_code")
	order_num := c.PostForm("order_num")
	order_code := c.PostForm("order_code")
	cid, _ := strconv.Atoi(c.PostForm("cid"))
	order := models.Order{
		OrderNo:   order_no,
		OrderNum:  order_num,
		OrderCode: order_code,
		Cid:       cid,
	}
	models.DB.Create(&order)
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "添加成功",
	})
	return
}

func OrderList(c *gin.Context) {
	page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
	size, _ := strconv.Atoi(c.DefaultQuery("size", "10"))
	offset := (page - 1) * size
	var count int64
	models.DB.Model(&models.Order{}).Count(&count)
	var carOrder []models.CarOrder
	/*
		SELECT
		  orders.id AS OrderID,
		  orders.order_no AS OrderNo,
		  orders.order_num AS OrderNum,
		  orders.order_code AS OrderCode,
		  cars.car_code AS CarCode,
		  cars.car_no AS CarNo,
		  cars.car_name AS CarName
		FROM
		  orders
		  LEFT JOIN cars ON cars.id = orders.cid
	*/
	redis_key := "order_list_" + strconv.Itoa(page)
	cacheDara, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()
	if cacheDara == "" {
		models.DB.Table("orders").
			Select(
				"orders.id AS OrderID",
				"orders.order_no AS OrderNo",
				"orders.order_num AS OrderNum",
				"orders.order_code AS OrderCode",
				"cars.car_code AS CarCode",
				"cars.car_no AS CarNo",
				"cars.car_name AS CarName",
			).
			Joins("LEFT JOIN cars ON cars.id = orders.cid").
			Offset(offset).
			Limit(size).
			Find(&carOrder)
		jsonDara, _ := json.Marshal(carOrder)
		cache.Rdb.Set(cache.Ctx, redis_key, jsonDara, time.Hour*2)
	} else {
		json.Unmarshal([]byte(cacheDara), &carOrder)
	}

	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "查询成功",
		"data": carOrder,
		//"count": count,
	})
	return
}

func OrderDetail(c *gin.Context) {
	id := c.Query("id")
	var carOrder models.CarOrder
	/*
		SELECT
		  orders.id AS OrderID,
		  orders.order_no AS OrderNo,
		  orders.order_num AS OrderNum,
		  orders.order_code AS OrderCode,
		  cars.car_code AS CarCode,
		  cars.car_no AS CarNo,
		  cars.car_name AS CarName
		FROM
		  orders
		  LEFT JOIN cars ON cars.id = orders.cid
	*/
	redis_key := "order_detail_" + id
	cacheDara, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()
	if cacheDara == "" {
		models.DB.Table("orders").
			Select(
				"orders.id AS OrderID",
				"orders.order_no AS OrderNo",
				"orders.order_num AS OrderNum",
				"orders.order_code AS OrderCode",
				"cars.car_code AS CarCode",
				"cars.car_no AS CarNo",
				"cars.car_name AS CarName",
			).
			Joins("LEFT JOIN cars ON cars.id = orders.cid").
			Where("order.id = ?", id).
			Find(&carOrder)
		jsonDara, _ := json.Marshal(carOrder)
		cache.Rdb.Set(cache.Ctx, redis_key, jsonDara, time.Hour*2)
	} else {
		json.Unmarshal([]byte(cacheDara), &carOrder)
	}

	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "查询成功",
		"data": carOrder,
	})
	return
}
